/*
 * Copyright (c) 2008, Yifu Huang
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of SleekIce nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package com.sleekice.object;

/* acceleration for objects */
public class Acceleration {

    /* the defaults */
    private static final double DEF_MAGNITUDE = 0;
    private static final double DEF_DIRECTION = 0;
    
    private double magnitude_;
    private double direction_;
    
    public Acceleration() {
        this(DEF_DIRECTION, DEF_MAGNITUDE);
    }
    
    public Acceleration(double direction) {
        this(direction, DEF_MAGNITUDE);
    }
    
    public Acceleration(double direction, double magnitude) {
        /* make sure dir is set within range */
        setDirection(direction);
        magnitude_ = magnitude;
    }
    
    public void addVector(double direction, double magnitude) {
        double hMagnInc = Math.cos(Math.toRadians(direction)) * magnitude;
        double vMagnInc = Math.sin(Math.toRadians(direction)) * magnitude;
        double newHMagn = Math.cos(Math.toRadians(direction_)) * magnitude_+hMagnInc;
        double newVMagn = Math.sin(Math.toRadians(direction_)) * magnitude_+vMagnInc;
        double newMagn = Math.sqrt(Math.pow(newHMagn, 2)+Math.pow(newVMagn,2));
        double newDirection = Math.toDegrees(Math.atan2(newVMagn,newHMagn));
        setDirection(newDirection);
        setMagnitude(newMagn);
    }

    // initial work on acceleration from any direction
    // sets the new velocity over a given delta time
    public void updateVelocity(Velocity velocity, double elapsedTime) {
        if (magnitude_ == 0) {
            return;
        }
        double speedInc = elapsedTime * magnitude_;
        double hSpeedInc = Math.cos(Math.toRadians(direction_)) * speedInc;
        double vSpeedInc = Math.sin(Math.toRadians(direction_)) * speedInc;
        double newHSpeed = velocity.getHSpeed()+hSpeedInc;
        double newVSpeed = velocity.getVSpeed()+vSpeedInc;
        double newSpeed = Math.sqrt(Math.pow(newHSpeed, 2)+Math.pow(newVSpeed,2));
        double newDirection = Math.toDegrees(Math.atan2(newVSpeed,newHSpeed));
        velocity.setDirection(newDirection);
        velocity.setSpeed(newSpeed);
    }

    public double getMagnitude() {
        return magnitude_;
    }
    
    public double getDirection() {
        return direction_;
    }
    
    public void setMagnitude(double magnitude) {
        magnitude_ = magnitude;
    }
    
    public void setDirection(double direction) {
        direction_ = direction % 360;
    }
    
    @Override
    public String toString() {
        return "Acceleration {Direction = "+direction_+"|Speed = "+magnitude_+"}";
    }
}